//@author A0088498A
package common;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Supported Attribute Type <br/>
 * Define all the Attribute Type that a Task can have <br/>
 * Some AttributeTypes would be synonyms (exchangeable) <br/>
 * 
 */

public enum AttributeType {
	id, name, type, description, startingTime, from, endingTime, to, due, as, range, status, invalid;

	/**
	 * Convert the String into the root AttributeType <br/>
	 * That means if the string is "to", "startingTime" is returned (as
	 * "startingTime is the root of "to") <br/>
	 * 
	 * @param string
	 *            to be converted
	 * @return the AttributeType converted. Return invalid if the string is null
	 *         or cannot be converted
	 */
	public static AttributeType getValueOf(String string) {
		try {
			AttributeType immediateResult = valueOf(string);
			return reduceToRoot (immediateResult);
		} catch (RuntimeException e) {
			return invalid;
		}
	}

	/**
	 * Reduce a AttributeType to its root (standard synonym) <br/>
	 * 
	 * @param type
	 *            to be reduced
	 * @return the standard synonym of the type
	 */
	public static AttributeType reduceToRoot(AttributeType type) {
		AttributeType root = type;

		switch (type) {
		case from:
			root = startingTime;
			break;
		case to:
		case due:
		case as:
			root = endingTime;
			break;
		default:
			break;
		}

		return root;
	}

	/**
	 * Search for the keyword with the prefix provided <br/>
	 * 
	 * @param prefix
	 *            of the AttributeType to be search
	 * @return a list of attribute type having the prefix, sorted alphabetically
	 */
	public static ArrayList<String> searchKeyword(String prefix) {
		ArrayList<String> keywords = new ArrayList<String>();

		AttributeType[] attributeTypes = AttributeType.values();
		for (AttributeType attr : attributeTypes) {
			String keyword = attr.toString();
			if (keyword.startsWith(prefix)) {
				keywords.add(keyword);
			}
		}
		Collections.sort(keywords);

		return keywords;
	}

}
